{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9e84cd2f-dd3d-4f34-80a8-bb81125cc5a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import random\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "84d43c1c-7482-43a3-b099-9762e5af46f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from graphviz import Digraph\n",
    "\n",
    "def trace(root):\n",
    "    nodes, edges = set(), set()\n",
    "    def build(v):\n",
    "        if v not in nodes:\n",
    "            nodes.add(v)\n",
    "            for child in v._prev:\n",
    "                edges.add((child, v))\n",
    "                build(child)\n",
    "    build(root)\n",
    "    return nodes, edges\n",
    "\n",
    "def draw_dot(root):\n",
    "    nodes, edges = trace(root)\n",
    "    dot = Digraph(format='svg', graph_attr={'rankdir': 'LR'})\n",
    "\n",
    "    for n in nodes:\n",
    "        dot.node(name=str(id(n)), label = \"{ data %.4f | grad %.4f }\" % (n.data, n.grad), shape='record')\n",
    "        if n._op:\n",
    "            dot.node(name=str(id(n)) + n._op, label=n._op)\n",
    "            dot.edge(str(id(n)) + n._op, str(id(n)))\n",
    "\n",
    "    for n1, n2 in edges:\n",
    "        dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
    "\n",
    "    return dot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1124ffd0-a58a-4fc8-8823-7dbc191b1664",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Value:\n",
    "    def __init__(self, data, _children=(), _op=''):\n",
    "        self.data = data\n",
    "        self.grad = 0.0 # 梯度（向量）\n",
    "        self._backward = lambda: None # 反向传播函数，来执行梯度的自动传递（默认为空）\n",
    "        self._prev = set(_children) # 计算节点\n",
    "        self._op = _op # 计算符号\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f\"Value(data={self.data})\"\n",
    "\n",
    "    def __add__(self, other): # self 是加法的左操作数，other 是加法的右操作数\n",
    "        if isinstance(other, Value):\n",
    "            other = other # 如果 other 已经是 Value 类型，则保持原样\n",
    "        else:\n",
    "            other = Value(other) # 如果 other 不是 Value 类型，则将其包装成 Value 对象\n",
    "        \n",
    "        out = Value(self.data + other.data, (self, other), '+') # 创建 out 节点时建立的计算图（将 self 和 other 设为子节点）\n",
    "\n",
    "        def _backward():\n",
    "            self.grad += 1.0 * out.grad\n",
    "            other.grad += 1.0 * out.grad\n",
    "    \n",
    "        out._backward = _backward\n",
    "\n",
    "        return out # out 是加法的结果值\n",
    "\n",
    "    def __mul__(self, other):\n",
    "        if isinstance(other, Value):\n",
    "            other = other # 如果 other 已经是 Value 类型，则保持原样\n",
    "        else:\n",
    "            other = Value(other) # 如果 other 不是 Value 类型，则将其包装成 Value 对象\n",
    "\n",
    "        out = Value(self.data * other.data, (self, other), '*')\n",
    "\n",
    "        def _backward():\n",
    "            self.grad += other.data * out.grad\n",
    "            other.grad += self.data * out.grad\n",
    "\n",
    "        out._backward = _backward\n",
    "\n",
    "        return out\n",
    "\n",
    "    def tanh(self):\n",
    "        x = self.data\n",
    "        t = (math.exp(2*x) - 1) / (math.exp(2*x) + 1)\n",
    "        out = Value(t, (self, ), 'tanh')\n",
    "\n",
    "        def _backward():\n",
    "            self.grad += (1 - t**2) * out.grad\n",
    "    \n",
    "        out._backward = _backward\n",
    "\n",
    "        return out\n",
    "\n",
    "    def backward(self):\n",
    "        topo = [] # 存储拓扑排序结果\n",
    "        visited = set() # 记录已访问的节点\n",
    "\n",
    "        def build_topo(v):\n",
    "            if v not in visited: # 如果当前节点 v 没有被访问过，继续处理\n",
    "                visited.add(v) # 将 v 加入 visited 集合\n",
    "                for child in v._prev: # 对 v 的所有前驱节点(v._prev)递归调用 build_topo\n",
    "                    build_topo(child)\n",
    "                topo.append(v) # 当所有前驱节点都处理完后，将 v 加入 topo 列表\n",
    "\n",
    "        build_topo(self)\n",
    "\n",
    "        self.grad = 1.0\n",
    "        for node in reversed(topo):\n",
    "            node._backward()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cff374ed-b662-4ed5-ab65-e1ef681c9965",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 14.0.2 (20251019.1705)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"1331pt\" height=\"210pt\"\n",
       " viewBox=\"0.00 0.00 1331.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-206 1326.5,-206 1326.5,4 -4,4\"/>\n",
       "<!-- 2434655067136 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>2434655067136</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"583,-82.5 583,-118.5 748.5,-118.5 748.5,-82.5 583,-82.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"625.12\" y=\"-95.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"667.25,-83 667.25,-118.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"707.88\" y=\"-95.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655067568+ -->\n",
       "<g id=\"node15\" class=\"node\">\n",
       "<title>2434655067568+</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"811.5\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"811.5\" y=\"-122.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">+</text>\n",
       "</g>\n",
       "<!-- 2434655067136&#45;&gt;2434655067568+ -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>2434655067136&#45;&gt;2434655067568+</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M748.7,-115.89C757.5,-117.54 766.08,-119.16 773.92,-120.63\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"773.22,-124.06 783.7,-122.47 774.51,-117.18 773.22,-124.06\"/>\n",
       "</g>\n",
       "<!-- 2434655067136+ -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>2434655067136+</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"520\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"520\" y=\"-95.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">+</text>\n",
       "</g>\n",
       "<!-- 2434655067136+&#45;&gt;2434655067136 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>2434655067136+&#45;&gt;2434655067136</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M547.37,-100.5C554.47,-100.5 562.57,-100.5 571.14,-100.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"571.09,-104 581.09,-100.5 571.09,-97 571.09,-104\"/>\n",
       "</g>\n",
       "<!-- 2434655072272 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>2434655072272</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"0,-165.5 0,-201.5 165.5,-201.5 165.5,-165.5 0,-165.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"42.12\" y=\"-178.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"84.25,-166 84.25,-201.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"124.88\" y=\"-178.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655067328* -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>2434655067328*</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"228.5\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"228.5\" y=\"-123.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">*</text>\n",
       "</g>\n",
       "<!-- 2434655072272&#45;&gt;2434655067328* -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>2434655072272&#45;&gt;2434655067328*</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M142.08,-165.07C149.99,-162.33 157.98,-159.43 165.5,-156.5 175.41,-152.64 186.03,-148.02 195.62,-143.67\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"196.94,-146.92 204.56,-139.55 194.01,-140.56 196.94,-146.92\"/>\n",
       "</g>\n",
       "<!-- 2434655071792 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>2434655071792</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"585.25,-137.5 585.25,-173.5 746.25,-173.5 746.25,-137.5 585.25,-137.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"625.12\" y=\"-150.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 6.8814</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"665,-138 665,-173.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"705.62\" y=\"-150.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655071792&#45;&gt;2434655067568+ -->\n",
       "<g id=\"edge14\" class=\"edge\">\n",
       "<title>2434655071792&#45;&gt;2434655067568+</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M746.61,-139.94C756.1,-138.1 765.4,-136.28 773.84,-134.64\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"774.49,-138.08 783.64,-132.73 773.16,-131.21 774.49,-138.08\"/>\n",
       "</g>\n",
       "<!-- 2434655076016 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>2434655076016</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"2.25,-55.5 2.25,-91.5 163.25,-91.5 163.25,-55.5 2.25,-55.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"42.12\" y=\"-68.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"82,-56 82,-91.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"122.62\" y=\"-68.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655066992* -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>2434655066992*</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"228.5\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"228.5\" y=\"-68.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">*</text>\n",
       "</g>\n",
       "<!-- 2434655076016&#45;&gt;2434655066992* -->\n",
       "<g id=\"edge13\" class=\"edge\">\n",
       "<title>2434655076016&#45;&gt;2434655066992*</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M163.61,-73.5C172.73,-73.5 181.67,-73.5 189.85,-73.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"189.69,-77 199.69,-73.5 189.69,-70 189.69,-77\"/>\n",
       "</g>\n",
       "<!-- 2434655067328 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>2434655067328</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"291.5,-110.5 291.5,-146.5 457,-146.5 457,-110.5 291.5,-110.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"333.62\" y=\"-123.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"375.75,-111 375.75,-146.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"416.38\" y=\"-123.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655067328&#45;&gt;2434655067136+ -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>2434655067328&#45;&gt;2434655067136+</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M457.2,-112.54C466,-110.82 474.58,-109.15 482.42,-107.62\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"483.05,-111.07 492.2,-105.72 481.71,-104.2 483.05,-111.07\"/>\n",
       "</g>\n",
       "<!-- 2434655067328*&#45;&gt;2434655067328 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>2434655067328*&#45;&gt;2434655067328</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M255.87,-128.5C262.97,-128.5 271.07,-128.5 279.64,-128.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"279.59,-132 289.59,-128.5 279.59,-125 279.59,-132\"/>\n",
       "</g>\n",
       "<!-- 2434655069968 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>2434655069968</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"2.25,-0.5 2.25,-36.5 163.25,-36.5 163.25,-0.5 2.25,-0.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"42.12\" y=\"-13.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 1.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"82,-1 82,-36.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"122.62\" y=\"-13.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655069968&#45;&gt;2434655066992* -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>2434655069968&#45;&gt;2434655066992*</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M139.38,-36.88C148.16,-39.97 157.11,-43.24 165.5,-46.5 175.28,-50.3 185.79,-54.73 195.32,-58.9\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"193.64,-61.98 204.2,-62.83 196.47,-55.58 193.64,-61.98\"/>\n",
       "</g>\n",
       "<!-- 2434655066992 -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>2434655066992</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"293.75,-55.5 293.75,-91.5 454.75,-91.5 454.75,-55.5 293.75,-55.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"333.62\" y=\"-68.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"373.5,-56 373.5,-91.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"414.12\" y=\"-68.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655066992&#45;&gt;2434655067136+ -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>2434655066992&#45;&gt;2434655067136+</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M455.11,-88.5C464.6,-90.28 473.9,-92.03 482.34,-93.61\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"481.66,-97.05 492.14,-95.45 482.96,-90.17 481.66,-97.05\"/>\n",
       "</g>\n",
       "<!-- 2434655066992*&#45;&gt;2434655066992 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>2434655066992*&#45;&gt;2434655066992</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M255.87,-73.5C263.67,-73.5 272.7,-73.5 282.2,-73.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"282.02,-77 292.02,-73.5 282.02,-70 282.02,-77\"/>\n",
       "</g>\n",
       "<!-- 2434655069584 -->\n",
       "<g id=\"node11\" class=\"node\">\n",
       "<title>2434655069584</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"1161.5,-109.5 1161.5,-145.5 1322.5,-145.5 1322.5,-109.5 1161.5,-109.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"1201.38\" y=\"-122.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.7071</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"1241.25,-110 1241.25,-145.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"1281.88\" y=\"-122.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655069584tanh -->\n",
       "<g id=\"node12\" class=\"node\">\n",
       "<title>2434655069584tanh</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"1098.5\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"1098.5\" y=\"-122.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">tanh</text>\n",
       "</g>\n",
       "<!-- 2434655069584tanh&#45;&gt;2434655069584 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>2434655069584tanh&#45;&gt;2434655069584</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1125.78,-127.5C1132.9,-127.5 1141.02,-127.5 1149.58,-127.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1149.54,-131 1159.54,-127.5 1149.54,-124 1149.54,-131\"/>\n",
       "</g>\n",
       "<!-- 2434655068576 -->\n",
       "<g id=\"node13\" class=\"node\">\n",
       "<title>2434655068576</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"2.25,-110.5 2.25,-146.5 163.25,-146.5 163.25,-110.5 2.25,-110.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"42.12\" y=\"-123.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 2.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"82,-111 82,-146.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"122.62\" y=\"-123.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655068576&#45;&gt;2434655067328* -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>2434655068576&#45;&gt;2434655067328*</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M163.61,-128.5C172.73,-128.5 181.67,-128.5 189.85,-128.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"189.69,-132 199.69,-128.5 189.69,-125 189.69,-132\"/>\n",
       "</g>\n",
       "<!-- 2434655067568 -->\n",
       "<g id=\"node14\" class=\"node\">\n",
       "<title>2434655067568</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"874.5,-109.5 874.5,-145.5 1035.5,-145.5 1035.5,-109.5 874.5,-109.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"914.38\" y=\"-122.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.8814</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"954.25,-110 954.25,-145.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"994.88\" y=\"-122.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655067568&#45;&gt;2434655069584tanh -->\n",
       "<g id=\"edge12\" class=\"edge\">\n",
       "<title>2434655067568&#45;&gt;2434655069584tanh</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1035.85,-127.5C1044.24,-127.5 1052.46,-127.5 1060.02,-127.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1059.78,-131 1069.78,-127.5 1059.78,-124 1059.78,-131\"/>\n",
       "</g>\n",
       "<!-- 2434655067568+&#45;&gt;2434655067568 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>2434655067568+&#45;&gt;2434655067568</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M838.78,-127.5C845.9,-127.5 854.02,-127.5 862.58,-127.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"862.54,-131 872.54,-127.5 862.54,-124 862.54,-131\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x236dcc02560>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化所有梯度\n",
    "x1 = Value(2.0)\n",
    "x2 = Value(0.0)\n",
    "w1 = Value(-3.0)\n",
    "w2 = Value(1.0)\n",
    "b = Value(6.88137)\n",
    "x1w1 = x1*w1\n",
    "x2w2 = x2*w2\n",
    "x1w1x2w2 = x1w1 + x2w2\n",
    "n = x1w1x2w2 + b\n",
    "o = n.tanh()\n",
    "\n",
    "# 画出计算图\n",
    "draw_dot(o)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "623d7a46-2dfc-4ffd-9764-bc011588c1a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 14.0.2 (20251019.1705)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"1331pt\" height=\"210pt\"\n",
       " viewBox=\"0.00 0.00 1331.00 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-206 1326.5,-206 1326.5,4 -4,4\"/>\n",
       "<!-- 2434655067136 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>2434655067136</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"583,-82.5 583,-118.5 748.5,-118.5 748.5,-82.5 583,-82.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"625.12\" y=\"-95.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"667.25,-83 667.25,-118.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"707.88\" y=\"-95.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
       "</g>\n",
       "<!-- 2434655067568+ -->\n",
       "<g id=\"node15\" class=\"node\">\n",
       "<title>2434655067568+</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"811.5\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"811.5\" y=\"-122.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">+</text>\n",
       "</g>\n",
       "<!-- 2434655067136&#45;&gt;2434655067568+ -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>2434655067136&#45;&gt;2434655067568+</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M748.7,-115.89C757.5,-117.54 766.08,-119.16 773.92,-120.63\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"773.22,-124.06 783.7,-122.47 774.51,-117.18 773.22,-124.06\"/>\n",
       "</g>\n",
       "<!-- 2434655067136+ -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>2434655067136+</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"520\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"520\" y=\"-95.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">+</text>\n",
       "</g>\n",
       "<!-- 2434655067136+&#45;&gt;2434655067136 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>2434655067136+&#45;&gt;2434655067136</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M547.37,-100.5C554.47,-100.5 562.57,-100.5 571.14,-100.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"571.09,-104 581.09,-100.5 571.09,-97 571.09,-104\"/>\n",
       "</g>\n",
       "<!-- 2434655072272 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>2434655072272</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"0,-165.5 0,-201.5 165.5,-201.5 165.5,-165.5 0,-165.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"42.12\" y=\"-178.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data &#45;3.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"84.25,-166 84.25,-201.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"124.88\" y=\"-178.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
       "</g>\n",
       "<!-- 2434655067328* -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>2434655067328*</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"228.5\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"228.5\" y=\"-123.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">*</text>\n",
       "</g>\n",
       "<!-- 2434655072272&#45;&gt;2434655067328* -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>2434655072272&#45;&gt;2434655067328*</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M142.08,-165.07C149.99,-162.33 157.98,-159.43 165.5,-156.5 175.41,-152.64 186.03,-148.02 195.62,-143.67\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"196.94,-146.92 204.56,-139.55 194.01,-140.56 196.94,-146.92\"/>\n",
       "</g>\n",
       "<!-- 2434655071792 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>2434655071792</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"585.25,-137.5 585.25,-173.5 746.25,-173.5 746.25,-137.5 585.25,-137.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"625.12\" y=\"-150.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 6.8814</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"665,-138 665,-173.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"705.62\" y=\"-150.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
       "</g>\n",
       "<!-- 2434655071792&#45;&gt;2434655067568+ -->\n",
       "<g id=\"edge14\" class=\"edge\">\n",
       "<title>2434655071792&#45;&gt;2434655067568+</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M746.61,-139.94C756.1,-138.1 765.4,-136.28 773.84,-134.64\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"774.49,-138.08 783.64,-132.73 773.16,-131.21 774.49,-138.08\"/>\n",
       "</g>\n",
       "<!-- 2434655076016 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>2434655076016</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"2.25,-55.5 2.25,-91.5 163.25,-91.5 163.25,-55.5 2.25,-55.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"42.12\" y=\"-68.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"82,-56 82,-91.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"122.62\" y=\"-68.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
       "</g>\n",
       "<!-- 2434655066992* -->\n",
       "<g id=\"node10\" class=\"node\">\n",
       "<title>2434655066992*</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"228.5\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"228.5\" y=\"-68.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">*</text>\n",
       "</g>\n",
       "<!-- 2434655076016&#45;&gt;2434655066992* -->\n",
       "<g id=\"edge13\" class=\"edge\">\n",
       "<title>2434655076016&#45;&gt;2434655066992*</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M163.61,-73.5C172.73,-73.5 181.67,-73.5 189.85,-73.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"189.69,-77 199.69,-73.5 189.69,-70 189.69,-77\"/>\n",
       "</g>\n",
       "<!-- 2434655067328 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>2434655067328</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"291.5,-110.5 291.5,-146.5 457,-146.5 457,-110.5 291.5,-110.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"333.62\" y=\"-123.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data &#45;6.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"375.75,-111 375.75,-146.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"416.38\" y=\"-123.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
       "</g>\n",
       "<!-- 2434655067328&#45;&gt;2434655067136+ -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>2434655067328&#45;&gt;2434655067136+</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M457.2,-112.54C466,-110.82 474.58,-109.15 482.42,-107.62\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"483.05,-111.07 492.2,-105.72 481.71,-104.2 483.05,-111.07\"/>\n",
       "</g>\n",
       "<!-- 2434655067328*&#45;&gt;2434655067328 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>2434655067328*&#45;&gt;2434655067328</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M255.87,-128.5C262.97,-128.5 271.07,-128.5 279.64,-128.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"279.59,-132 289.59,-128.5 279.59,-125 279.59,-132\"/>\n",
       "</g>\n",
       "<!-- 2434655069968 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>2434655069968</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"2.25,-0.5 2.25,-36.5 163.25,-36.5 163.25,-0.5 2.25,-0.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"42.12\" y=\"-13.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 1.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"82,-1 82,-36.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"122.62\" y=\"-13.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0000</text>\n",
       "</g>\n",
       "<!-- 2434655069968&#45;&gt;2434655066992* -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>2434655069968&#45;&gt;2434655066992*</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M139.38,-36.88C148.16,-39.97 157.11,-43.24 165.5,-46.5 175.28,-50.3 185.79,-54.73 195.32,-58.9\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"193.64,-61.98 204.2,-62.83 196.47,-55.58 193.64,-61.98\"/>\n",
       "</g>\n",
       "<!-- 2434655066992 -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>2434655066992</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"293.75,-55.5 293.75,-91.5 454.75,-91.5 454.75,-55.5 293.75,-55.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"333.62\" y=\"-68.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"373.5,-56 373.5,-91.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"414.12\" y=\"-68.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
       "</g>\n",
       "<!-- 2434655066992&#45;&gt;2434655067136+ -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>2434655066992&#45;&gt;2434655067136+</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M455.11,-88.5C464.6,-90.28 473.9,-92.03 482.34,-93.61\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"481.66,-97.05 492.14,-95.45 482.96,-90.17 481.66,-97.05\"/>\n",
       "</g>\n",
       "<!-- 2434655066992*&#45;&gt;2434655066992 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>2434655066992*&#45;&gt;2434655066992</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M255.87,-73.5C263.67,-73.5 272.7,-73.5 282.2,-73.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"282.02,-77 292.02,-73.5 282.02,-70 282.02,-77\"/>\n",
       "</g>\n",
       "<!-- 2434655069584 -->\n",
       "<g id=\"node11\" class=\"node\">\n",
       "<title>2434655069584</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"1161.5,-109.5 1161.5,-145.5 1322.5,-145.5 1322.5,-109.5 1161.5,-109.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"1201.38\" y=\"-122.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.7071</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"1241.25,-110 1241.25,-145.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"1281.88\" y=\"-122.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\n",
       "</g>\n",
       "<!-- 2434655069584tanh -->\n",
       "<g id=\"node12\" class=\"node\">\n",
       "<title>2434655069584tanh</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"1098.5\" cy=\"-127.5\" rx=\"27\" ry=\"18\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"1098.5\" y=\"-122.45\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">tanh</text>\n",
       "</g>\n",
       "<!-- 2434655069584tanh&#45;&gt;2434655069584 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>2434655069584tanh&#45;&gt;2434655069584</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1125.78,-127.5C1132.9,-127.5 1141.02,-127.5 1149.58,-127.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1149.54,-131 1159.54,-127.5 1149.54,-124 1149.54,-131\"/>\n",
       "</g>\n",
       "<!-- 2434655068576 -->\n",
       "<g id=\"node13\" class=\"node\">\n",
       "<title>2434655068576</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"0,-110.5 0,-146.5 165.5,-146.5 165.5,-110.5 0,-110.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"39.88\" y=\"-123.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 2.0000</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"79.75,-111 79.75,-146.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"122.62\" y=\"-123.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad &#45;1.5000</text>\n",
       "</g>\n",
       "<!-- 2434655068576&#45;&gt;2434655067328* -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>2434655068576&#45;&gt;2434655067328*</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M165.7,-128.5C174.14,-128.5 182.4,-128.5 189.98,-128.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"189.77,-132 199.77,-128.5 189.77,-125 189.77,-132\"/>\n",
       "</g>\n",
       "<!-- 2434655067568 -->\n",
       "<g id=\"node14\" class=\"node\">\n",
       "<title>2434655067568</title>\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"874.5,-109.5 874.5,-145.5 1035.5,-145.5 1035.5,-109.5 874.5,-109.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"914.38\" y=\"-122.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.8814</text>\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"954.25,-110 954.25,-145.5\"/>\n",
       "<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"994.88\" y=\"-122.7\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.5000</text>\n",
       "</g>\n",
       "<!-- 2434655067568&#45;&gt;2434655069584tanh -->\n",
       "<g id=\"edge12\" class=\"edge\">\n",
       "<title>2434655067568&#45;&gt;2434655069584tanh</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1035.85,-127.5C1044.24,-127.5 1052.46,-127.5 1060.02,-127.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1059.78,-131 1069.78,-127.5 1059.78,-124 1059.78,-131\"/>\n",
       "</g>\n",
       "<!-- 2434655067568+&#45;&gt;2434655067568 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>2434655067568+&#45;&gt;2434655067568</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M838.78,-127.5C845.9,-127.5 854.02,-127.5 862.58,-127.5\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"862.54,-131 872.54,-127.5 862.54,-124 862.54,-131\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x236dcc022f0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自动化梯度计算\n",
    "o.backward()\n",
    "\n",
    "# 画出计算图\n",
    "draw_dot(o)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d1d9456-0d29-4dfb-9d5c-01f74c550232",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
